<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Repository\Smilapp\SmilappNoticeRepository;
use App\Repository\Smilapp\SmilappCategoryRepository;
use App\Repository\Smilapp\SmilappFlyerRepository;
use App\Repository\Smilapp\SmilappKefuRepository;
use App\Repository\Smilapp\SmilappLunRepository;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class AppSmailController extends BaseController
{
  private $NoticeModel;
  private $CategoryModel;
  private $LunModel;
  private $KefuModel;
  private $FlyerModel;

  public function __construct(SmilappNoticeRepository $SmilappNoticeRepository, SmilappCategoryRepository $SmilappCategoryRepository, SmilappFlyerRepository $SmilappFlyerRepository, SmilappKefuRepository $SmilappKefuRepository, SmilappLunRepository $SmilappLunRepository)
  {
    $this->NoticeModel = $SmilappNoticeRepository;
    $this->CategoryModel = $SmilappCategoryRepository;
    $this->FlyerModel = $SmilappFlyerRepository;
    $this->KefuModel = $SmilappKefuRepository;
    $this->LunModel = $SmilappLunRepository;
  }
  public function getSmailappLun(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    $where = [
      'where' => [
        'shop_id' => $shop_id,
        'status' => 0
        // 'is_app' => 0,
        // 'is_app_all' => 0
        // 'is_applets' => 0
      ],
      'limit' => [
        'start' => 0,
        'number' => 20
      ],
      'order' => [
        'orderby' => 'sort',
        'orderdir' => 'desc'
      ]
    ];
    $allData = $this->LunModel->sel($where);
    // 将图片 处理成 绝对路径
    $return = [];
    if (!empty($allData['data'])) {
      foreach ($allData['data'] as $key => &$value) {
        $return[] = $path . $url . $value['main_pic'];
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function getSmailappCategory(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    $where = [
      'where' => [
        'shop_id' => $shop_id,
        // 'is_app' => 0,
        // 'is_app_all' => 0
        // 'is_applets' => 0
      ],
      'limit' => [
        'start' => 0,
        'number' => 20
      ],
      'order' => [
        'orderby' => 'sort',
        'orderdir' => 'desc'
      ]
    ];
    $allData = $this->CategoryModel->sel($where);
    // 将图片 处理成 绝对路径
    $return = [];
    if (!empty($allData['data'])) {
      foreach ($allData['data'] as $key => &$value) {
        // $return[] = $path . $url . $value['main_pic'];
        $return[] = [
          'url' => $path . $url . $value['main_pic'],
          'text' => $value['name'],
          'id' => $value['id']
        ];
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function getSmailappFlyer(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    // $where = [
    //   'where' => [
    //     'shop_id' => $shop_id,
    //     // 'is_app' => 0,
    //     // 'is_app_all' => 0
    //     // 'is_applets' => 0
    //   ],
    //   'limit' => [
    //     'start' => 0,
    //     'number' => 20
    //   ],
    //   'order' => [
    //     'orderby' => 'sort',
    //     'orderdir' => 'desc'
    //   ]
    // ];
    $where = [
      'status' => 0,
      'shop_id' => $shop_id
    ];
    // DB::connection()->enableQueryLog();
    $allData = $this->FlyerModel->getOne($where);
    // $logs = DB::getQueryLog();
    // var_dump($logs);
    // 将图片 处理成 绝对路径
    $return = [];
    // if (!empty($allData['data'])) {
    //   foreach ($allData['data'] as $key => &$value) {
    //     // $return[] = $path . $url . $value['main_pic'];
    //     $return = [
    //       'url' => $path . $url . $value['main_pic'],
    //       'text' => $value['name'],
    //       'id' => $value['id']
    //     ];
    //   }
    // }
    if (!empty($allData['main_pic'])) {
      $allData['main_pic'] = $path . $url . $allData['main_pic'];
    }
    return $this->returnMsg(200, '查询成功', $allData);
  }
  public function getSmailappNotice(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    $where = [
      'where' => [
        'shop_id' => $shop_id,
        // 'is_app' => 0,
        // 'is_app_all' => 0
        // 'is_applets' => 0
      ],
      'limit' => [
        'start' => 0,
        'number' => 20
      ],
      'order' => [
        'orderby' => 'sort',
        'orderdir' => 'desc'
      ]
    ];
    $allData = $this->NoticeModel->sel($where);
    // 将图片 处理成 绝对路径
    $return = [];
    if (!empty($allData['data'])) {
      foreach ($allData['data'] as $key => &$value) {
        // $return[] = $path . $url . $value['main_pic'];
        $return[] = [
          'url' => $path . $url . $value['main_pic'],
          'name' => $value['name'],
          'id' => $value['id'],
        ];
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
  public function getSmailappKefu(Request $request)
  {

    $shop_id = $request->get('shopId');
    if (empty($shop_id)) {
      return $this->returnMsg(100, '参数错误,请确认正确的连接');
    }

    $url = $_SERVER['HTTP_HOST'];
    // var_dump($url);
    $path = 'http://';
    if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == '443') {
      $path = 'https://';
    }

    // $shop_id = 1;
    $where = [
      'where' => [
        'shop_id' => $shop_id,
        // 'is_app' => 0,
        // 'is_app_all' => 0
        // 'is_applets' => 0
      ],
      'limit' => [
        'start' => 0,
        'number' => 20
      ],
      'order' => [
        'orderby' => 'sort',
        'orderdir' => 'desc'
      ]
    ];
    $allData = $this->KefuModel->sel($where);
    // 将图片 处理成 绝对路径
    $return = [];
    if (!empty($allData['data'])) {
      foreach ($allData['data'] as $key => &$value) {
        $return[] = $path . $url . $value['main_pic'];
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
}
